CXX = clang++
CXXFLAGS = -std=c++17 -Wall -Wextra -Iinclude
SRCDIR = src
OBJDIR = obj
BINDIR = bin

SOURCES = $(wildcard $(SRCDIR)/*.cpp)
OBJECTS = $(SOURCES:$(SRCDIR)/%.cpp=$(OBJDIR)/%.o)
TARGET = $(BINDIR)/c-compiler

.PHONY: all clean test

all: $(TARGET)

$(TARGET): $(OBJECTS) | $(BINDIR)
	$(CXX) $(OBJECTS) -o $@

$(OBJDIR)/%.o: $(SRCDIR)/%.cpp | $(OBJDIR)
	$(CXX) $(CXXFLAGS) -c $< -o $@

$(OBJDIR):
	mkdir -p $(OBJDIR)

$(BINDIR):
	mkdir -p $(BINDIR)

clean:
	rm -rf $(OBJDIR) $(BINDIR)

test: $(TARGET)
	@echo "Testing hello.c..."
	./$(TARGET) examples/hello.c examples/hello.s
	@echo "Testing arithmetic.c..."
	./$(TARGET) examples/arithmetic.c examples/arithmetic.s
	@echo "Testing control_flow.c..."
	./$(TARGET) examples/control_flow.c examples/control_flow.s
	@echo "Tests completed!"

install: $(TARGET)
	cp $(TARGET) /usr/local/bin/c-compiler

.PHONY: help
help:
	@echo "Available targets:"
	@echo "  all     - Build the compiler"
	@echo "  clean   - Remove build artifacts"
	@echo "  test    - Run example programs"
	@echo "  install - Install compiler to /usr/local/bin"
	@echo "  help    - Show this help message"
